<template>
    <div>
        <h1 style="margin-left: 30px">财务统计</h1>
    <el-row style="margin-left: 10px">
        <el-col :span="12">
            <div style="margin-left: 20px;margin-top: 20px">
                <span>日期：</span>
                <el-date-picker
                        v-model="methodorYear.starttime"
                        type="date" size="medium" value-format="yyyy-MM-dd">
                </el-date-picker>


                <el-date-picker
                        v-model="methodorYear.endtime"
                        type="date" size="medium" value-format="yyyy-MM-dd">
                </el-date-picker>
                <el-button type="primary" icon="el-icon-search" style="margin-left: 10px" size="medium"
                           @click="myIncomeList()">检索
                </el-button>
            </div>
        </el-col>
        <el-col :span="12">
            <div style="margin-left: 30px;margin-top: 20px">
                <el-button size="medium" @click="nearAMonth">近一个月</el-button>
                <el-button size="medium" @click="nearThreeMonth">近三个月</el-button>
                <el-button size="medium" @click="nearHalfYear">近半年</el-button>
                <el-button size="medium" @click="nearAYear">近一年</el-button>
            </div>
        </el-col>
    </el-row>
    <div ref="main" style="width: 80%;height:600px;margin-left: 30px;margin-top: 50px"></div>
    </div>
</template>

<script>
    import echarts from 'echarts'
    import CwInoutTypeService from '../../../model/ww/cw_inout_type/CwInoutTypeService'

    const cwInoutTypeService=CwInoutTypeService.getInstance();


    export default {
        name: "FinanceCount",
        data(){
            return {
                methodorYear:{
                    starttime:'',
                    endtime:''
                }, //财务统计对象
                datasource:[],    //数据内容
                cwnames:[]
            }
        },
        methods:{
            datass(){
                // 基于准备好的dom，初始化echarts实例
                let myChart = echarts.init(this.$refs['main'])
                // 绘制图表
                myChart.setOption({
                    title: {
                        text: '收入总览',
                        subtext: '收支类型',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b} : {c} ({d}%)'
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data:this.cwnames
                    },
                    series: [
                        {
                            name: '收入来源',
                            type: 'pie',
                            radius: '55%',
                            center: ['50%', '60%'],
                            data: this.datasource,
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                }
            );
            },
            myIncomeList(){ //按搜索框查询
                cwInoutTypeService.ssFinanceCount(this.methodorYear).then((response)=>{
                     this.datasource=[]
                    this.cwnames=[]
                    for(let i=0;i<response.data.data.length;i++){
                        this.datasource.push({value:response.data.data[i].a,name:response.data.data[i].cw_name})
                    }
                    for(let i=0;i<this.datasource.length;i++){
                        this.cwnames.push(this.datasource[i].cw_name);
                    }
                }).then(()=>{this.datass()})

            }
            ,
            nearAMonth(){
                this.methodorYear.starttime=''
                this.methodorYear.endtime=''
                var date=new Date();
                var year;
                var month;
                if((date.getMonth()+1)==1){
                    year=date.getFullYear()-1;
                    month=12;
                }else{
                    year=date.getFullYear();
                    month=date.getMonth()-1;
                }

                this.methodorYear.starttime=year+"-"+(month+1)+"-"+date.getDate();
                this.methodorYear.endtime=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
                cwInoutTypeService.ssFinanceCount(this.methodorYear).then((response)=>{
                    this.datasource=[]
                    this.cwnames=[]
                    for(let i=0;i<response.data.data.length;i++){
                        this.datasource.push({value:response.data.data[i].a,name:response.data.data[i].cw_name})
                    }
                    for(let i=0;i<this.datasource.length;i++){
                        this.cwnames.push(this.datasource[i].cw_name);
                    }
                }).then(()=>{this.datass()})


            },
            nearThreeMonth(){
                this.methodorYear.starttime=''
                this.methodorYear.endtime=''
                var now = new Date();
                var year = now.getFullYear();
                var month = now.getMonth() + 1;//0-11表示1-12月
                var day = now.getDate();
                var dateObj = {};
                var last3MonthDay;
                dateObj.now = year + '-' + month + '-' + day;
                var nowMonthDay = new Date(year, month, 0).getDate();    //当前月的总天数
                if(month - 3 <= 0){ //如果是1、2、3月，年数往前推一年
                    last3MonthDay = new Date((year - 1), (12 - (3 - parseInt(month))), 0).getDate();    //3个月前所在月的总天数
                    if(last3MonthDay < day){    //3个月前所在月的总天数小于现在的天日期
                        dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + last3MonthDay;
                    }else{
                        dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + day;
                    }
                }else{
                    last3MonthDay = new Date(year, (parseInt(month) - 3), 0).getDate();    //3个月前所在月的总天数
                    if(last3MonthDay < day){    //3个月前所在月的总天数小于现在的天日期
                        if(day < nowMonthDay){        //当前天日期小于当前月总天数,2月份比较特殊的月份
                            dateObj.last = year + '-' + (month - 3) + '-' + (last3MonthDay - (nowMonthDay - day));
                        }else{
                            dateObj.last = year + '-' + (month - 3) + '-' + last3MonthDay;
                        }
                    }else{
                        dateObj.last = year + '-' + (month - 3) + '-' + day;
                    }
                }

                this.methodorYear.starttime=dateObj.last;
                this.methodorYear.endtime=now.getFullYear()+"-"+(now.getMonth()+1)+"-"+now.getDate();

                cwInoutTypeService.ssFinanceCount(this.methodorYear).then((response)=>{
                    this.datasource=[]
                    this.cwnames=[]
                    for(let i=0;i<response.data.data.length;i++){
                        this.datasource.push({value:response.data.data[i].a,name:response.data.data[i].cw_name})
                    }
                    for(let i=0;i<this.datasource.length;i++){
                        this.cwnames.push(this.datasource[i].cw_name);
                    }
                }).then(()=>{this.datass()})
            },
            nearHalfYear(){
                this.methodorYear.starttime=''
                this.methodorYear.endtime=''
                let data = new Date();
                // 获取年份
                let year = data.getFullYear();
                // 获取月份
                let mon = data.getMonth() + 1;
                let array = new Array();
                for (let i = 0; i < 6; i++) {
                    mon = mon - 1;
                    if (mon <= 0) {
                        year = year - 1;
                        mon = mon + 12;
                    }
                    if (mon < 10) {
                        mon = "0" + mon;
                    }
                    array[i] = year + "-" + mon + "-" + data.getDate();
                }
                this.methodorYear.starttime=array[array.length-1];
                this.methodorYear.endtime=data.getFullYear()+"-"+(data.getMonth()+1)+"-"+data.getDate();
                cwInoutTypeService.ssFinanceCount(this.methodorYear).then((response)=>{
                    this.datasource=[]
                    this.cwnames=[]
                    for(let i=0;i<response.data.data.length;i++){
                        this.datasource.push({value:response.data.data[i].a,name:response.data.data[i].cw_name})
                    }
                    for(let i=0;i<this.datasource.length;i++){
                        this.cwnames.push(this.datasource[i].cw_name);
                    }
                }).then(()=>{this.datass()})
            },
            nearAYear(){
                this.methodorYear.starttime=''
                this.methodorYear.endtime=''
                var date=new Date();
                var lastYear=(date.getFullYear()-1)+"-"+(date.getMonth()+1)+"-"+date.getDate();
                this.methodorYear.starttime=lastYear;
                this.methodorYear.endtime=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
                cwInoutTypeService.ssFinanceCount(this.methodorYear).then((response)=>{
                    this.datasource=[]
                    this.cwnames=[]
                    for(let i=0;i<response.data.data.length;i++){
                        this.datasource.push({value:response.data.data[i].a,name:response.data.data[i].cw_name})
                    }
                    for(let i=0;i<this.datasource.length;i++){
                        this.cwnames.push(this.datasource[i].cw_name);
                    }
                }).then(()=>{this.datass()})
            },
        },
        created(){

        }

    }
</script>

<style scoped>

</style>
